package Queue;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;

public class MyQueue {

    // 用双链表实现队列： 先进先出
    public class LinkNode{
        public int val;
        public LinkNode next;
        public LinkNode prev;

        public LinkNode(int val){
            this.val=val;
        }
    }

    public LinkNode head;
    public LinkNode last;
    public int usedSize;

    // 入栈
    public boolean offer(int val){
        LinkNode node=new LinkNode(val);
        if(head==null){
            head=node;
            last=node;
        }else{
            last.next=node;
            node.prev=last;
            last=node;
        }
        usedSize++;
        return true;
    }

    // 出栈
    public int poll(){
        if(head==null){
            return -1;
        }
        LinkNode cur=head;
        if(head.next==null){
            head=null;
            last=null;
        }else{
        head=head.next;
        head.prev=null;
        }
        usedSize--;
        return cur.val;
    }

    public int peek(){
        if(head==null){
            return -1;
        }
        return head.val;
    }

    public int size(){
        return usedSize;
    }

    public boolean isEmpty(){
        return usedSize==0;
    }

}
